package com.ruoyi.ai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.ai.domain.PromotMsgUtils;
import com.ruoyi.ai.domain.vo.ChatCommonVo;
import com.ruoyi.ai.factory.ModelChatService;
import com.ruoyi.ai.factory.ModelChatStrategyFactory;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AiServiceImpl {


    public String chatStreamOnce(ChatCommonVo chatCommonVo){
        Long wxUserId = LoginHelper.getUserId();
        final String key = "report_nums" + wxUserId+ DateUtils.getDate();
        Integer nums= RedisUtils.getCacheObject(key);
        if(nums==null){
            nums=0;
        }else if(nums>=2){
            throw new ServiceException("今日报告次数已达上限，请明日再来");
        }
        RedisUtils.setCacheObject(key, ++nums);

        ModelChatService modelChatService = ModelChatStrategyFactory.getModelService(chatCommonVo.getModel());
        if(!ObjectUtils.isEmpty(chatCommonVo.getMsgType())&&!ObjectUtils.isEmpty(PromotMsgUtils.getPromotByType(chatCommonVo.getMsgType()))){
            chatCommonVo.setContent(chatCommonVo.getContent()+"</br>"+PromotMsgUtils.getPromotByType(chatCommonVo.getMsgType()));
        }
        String res = modelChatService.chatStreamOnce(chatCommonVo.getContent(),chatCommonVo);
        return res;
    }


}
